package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.DaoSchoolChildDao;
import com.sc.nft.entity.DaoSchoolChild;
import com.sc.nft.entity.DaoSchoolTop;
import com.sc.nft.entity.dto.DaoSchoolChildPageAdminDTO;
import com.sc.nft.entity.dto.DaoSchoolChildSaveAdminDTO;
import com.sc.nft.entity.dto.DaoSchoolChildTopGetListAllDTO;
import com.sc.nft.entity.dto.DaoSchoolChildUpdateByIdAdminDTO;
import com.sc.nft.entity.vo.DaoSchoolArticleMaterialTopIdVO;
import com.sc.nft.entity.vo.DaoSchoolChildPageAdminVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DaoSchoolChildService;
import com.sc.nft.service.DaoSchoolTopService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * dao学堂子分类表(DaoSchoolChild)表服务实现类
 *
 * @author makejava
 * @since 2023-03-13 16:21:50
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DaoSchoolChildServiceImpl implements DaoSchoolChildService {

    private final DaoSchoolChildDao daoSchoolChildDao;

    private final DaoSchoolTopService daoSchoolTopService;

    @Override
    public Page<DaoSchoolChildPageAdminVO> pageAdmin(DaoSchoolChildPageAdminDTO pageRequest) {
        return daoSchoolChildDao.pageAdmin(pageRequest);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAdmin(DaoSchoolChildSaveAdminDTO daoSchoolChildSaveAdminDTO) {
        DaoSchoolTop daoSchoolTop = daoSchoolTopService.selectById(daoSchoolChildSaveAdminDTO.getSecondTopId());
        if (BeanUtil.isEmpty(daoSchoolTop)) {
            throw new GlobalRunTimeException("父集不存在或已被删除");
        }
        // 名字不可以相同的.所有的名字必须去掉前后空格
        Boolean selectByNameCountIsExist = daoSchoolChildDao.selectByNameCountIsExist(daoSchoolChildSaveAdminDTO.getName().trim(), daoSchoolTop.getId());
        if (selectByNameCountIsExist) {
            throw new GlobalRunTimeException("存在同名数据,请更正后在进行操作");
        }
        DaoSchoolChild daoSchoolChild = new DaoSchoolChild();
        daoSchoolChild.setDaoId(6L);
        BeanUtil.copyProperties(daoSchoolChildSaveAdminDTO, daoSchoolChild);
        daoSchoolChild.setName(daoSchoolChild.getName().trim());
        daoSchoolChild.setArticleTotal(0);
        return daoSchoolChild.insert();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByIdAdmin(DaoSchoolChildUpdateByIdAdminDTO daoSchoolChildUpdateByIdAdminDTO) {
        DaoSchoolChild daoSchoolChild = daoSchoolChildDao.selectById(daoSchoolChildUpdateByIdAdminDTO.getId());
        if (BeanUtil.isEmpty(daoSchoolChild)) {
            throw new GlobalRunTimeException("数据不存在或已被删除");
        }
        // 参数保留一份.判定是否有空参数传入进来
        DaoSchoolChildUpdateByIdAdminDTO daoInfo = new DaoSchoolChildUpdateByIdAdminDTO();

        // 复制一份参数
        BeanUtil.copyProperties(daoSchoolChildUpdateByIdAdminDTO, daoInfo);

        // 必传参数设置为空以后判定对象是否存在
        daoSchoolChildUpdateByIdAdminDTO.setId(null);

        if (BeanUtil.isEmpty(daoSchoolChildUpdateByIdAdminDTO)) {
            throw new GlobalRunTimeException("无修改请求参数");
        }
        // 状态判定,如果这个状态是有值的话,则要做一些判定来对他进行处理.
        stateDecision(daoSchoolChildUpdateByIdAdminDTO, daoSchoolChild);

        if (!StrUtil.isEmpty(daoInfo.getName())) {
            Boolean selectByNameCountIsExistNotId = daoSchoolChildDao.selectByNameCountIsExistNotId(daoInfo.getName().trim(), daoInfo.getId(),daoSchoolChild.getSecondTopId());
            if (selectByNameCountIsExistNotId) {
                throw new GlobalRunTimeException("名称已被占用");
            }
        }
        return daoSchoolChildDao.updateById(daoInfo);
    }

    /**
     * 在用来修改的时候,判定状态是否有值,如果有值,则需要做一些判定来对他进行处理.
     *
     * @param daoSchoolChildUpdateByIdAdminDTO
     * @param daoSchoolChild
     */
    private void stateDecision(DaoSchoolChildUpdateByIdAdminDTO daoSchoolChildUpdateByIdAdminDTO, DaoSchoolChild daoSchoolChild) {
        if (daoSchoolChildUpdateByIdAdminDTO.getStatus() == null)
            return;
        //        如果状态为1,则需要判定是否有文章存在,存在需要把父分文章数扣掉
        if (daoSchoolChildUpdateByIdAdminDTO.getStatus().equals(1)) {
            daoSchoolTopService.totalSubSum(daoSchoolChild.getSecondTopId(), daoSchoolChild.getArticleTotal());
        }
//        如果状态为0,则需要判定是否有文章存在,存在需要把父分文章数加上
        if (daoSchoolChildUpdateByIdAdminDTO.getStatus().equals(0)) {
            daoSchoolTopService.totalAddSum(daoSchoolChild.getSecondTopId(), daoSchoolChild.getArticleTotal());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void totalAddOne(Long id) {
        daoSchoolChildDao.totalAddOne(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void totalSubOne(Long id) {
        daoSchoolChildDao.totalSubOne(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        DaoSchoolChild daoSchoolChild = daoSchoolChildDao.selectById(id);
        if (BeanUtil.isEmpty(daoSchoolChild)) {
            throw new GlobalRunTimeException("当前数据不存在");
        }
        if (daoSchoolChild.getArticleTotal() > 0) {
            throw new GlobalRunTimeException("不可删除当前分组,当前分组下子集中存在未删除的文章");
        }
        return daoSchoolChildDao.deleteById(id);
    }

    @Override
    public DaoSchoolChild selectById(Long id) {
        return daoSchoolChildDao.selectById(id);
    }

    /**
     * 根据父集ID获取子集列表
     *
     * @param daoSchoolChildTopGetListAllDTO
     * @return
     */
    @Override
    public List<DaoSchoolChildPageAdminVO> selectByTopIdsGetListAll(DaoSchoolChildTopGetListAllDTO daoSchoolChildTopGetListAllDTO) {
        return daoSchoolChildDao.selectByTopIdsGetListAll(daoSchoolChildTopGetListAllDTO);
    }

    /**
     * 根据父集id,获取子集列表
     *
     * @param topId
     * @return
     */
    @Override
    public List<DaoSchoolArticleMaterialTopIdVO> selectByTopId(Long topId) {

        return daoSchoolChildDao.selectByTopId(topId);
    }

}
